package cn.swh.guguanjia.entity;

import cn.swh.guguanjia.mapper.SysAreaMapper;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/11/2 16:55
 * @Version: 1.0
 */
public class SysAreaListener extends AnalysisEventListener<SysArea> {

    private List<SysArea> sysAreas=new ArrayList<>();

    private SysAreaMapper sysAreaMapper;

    public SysAreaListener(SysAreaMapper sysAreaMapper) {
        this.sysAreaMapper = sysAreaMapper;
    }

    public SysAreaListener() {
    }

    @Override
    public void invoke(SysArea data, AnalysisContext Context) {
//        System.out.println("读取一行");
        sysAreas.add(data);
        if(sysAreas.size()==10){
//            System.out.println("批量保存");
            sysAreaMapper.insetBatch(sysAreas);
            sysAreas.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext Context) {

        if(sysAreas.size()>0){
//            System.out.println("批量保存");
            sysAreaMapper.insetBatch(sysAreas);
            sysAreas.clear();
        }

    }
}
